package com.xy.frame.controller;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.xy.commons.PageUtil;
import com.xy.frame.model.AjaxJson;
import com.xy.frame.service.MenuService;

/**
 * @author Administrator
 * 菜单管理控制器
 */
@Controller
@RequestMapping("/menu")
public class MenuController {

	Logger logger = LoggerFactory.getLogger(MenuController.class);
    @Autowired
    MenuService menuService;
      
    /**
     * @param currentPage
     * @param pageSize
     * @return
     * 查询菜单列表数据
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson list(HttpServletRequest request,@RequestParam(value = "currentPage",defaultValue = "1") int currentPage,@RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        AjaxJson j = new AjaxJson();
        String menuLevel= request.getParameter("menuLevel");
        String menuName = request.getParameter("menuName");
        Map<String,Object> params = Maps.newHashMap();
        if(!StringUtils.isEmpty(menuLevel)){
        	params.put("menuLevel", menuLevel);
        }
        if(!StringUtils.isEmpty(menuName)){
        	params.put("menuName", menuName);
        }
        params.put("start",(currentPage -1)*pageSize);// > start
        params.put("end",currentPage*pageSize);  // <=  end
        Map<String,Object> pageList =  menuService.queryMenuListByPage(params);
        pageList.put(PageUtil.CURRENT,currentPage);
        pageList.put(PageUtil.PAGESIZE,pageSize);
        Integer t  = Integer.valueOf(pageList.get(PageUtil.TOTALS).toString());
        pageList.put(PageUtil.TOTALPAGE,PageUtil.getTotalPage(t,pageSize));
        j.setObj(pageList);
        if (logger.isDebugEnabled()) {
            logger.debug("请求【/menu/list】处理,响应对象【"+j+"】");
        }
        return j;
    }

    /**
     * @param type
     * @param model
     * @return
     * 查看、增加、修改菜单页面
     */
    @RequestMapping(value = "/toMenuPage", method = RequestMethod.GET)
    public String toMenuPage(@RequestParam("type") String type,Model model,HttpServletRequest request){
    	
        if (logger.isDebugEnabled()) {
            logger.debug("请求【/metro/menu/menuPage】处理页面【/view/metro/menu/menuPage.jsp】");
        }
        model.addAttribute("type",type);//查看-0，增加 -1，修改-2
        if("2".equals(type)){
        	String menuNum = request.getParameter("menuNum");
        	model.addAttribute("menuNum",menuNum);
        }
        return "metro/menu/menuPage";
    }
    /**
     * @param menuNum
     * @return
     * 获取一个菜单详细信息
     */
    @RequestMapping(value = "/getMenu", method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson getMenu(@RequestParam("menuNum") String menuNum){
        AjaxJson j = new AjaxJson();
       
        Map<String,Object> menuMap = menuService.getMenu(menuNum);
        j.setObj(menuMap);
        if (logger.isDebugEnabled()) {
            logger.debug("请求【/metro/menu/getMenu】处理,响应对象【"+j+"】");
        }
        return j;
    }

    /**
     * @return
     * 增加一个菜单
     */
    @RequestMapping(value = "/addMenu", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson addMenu(HttpServletRequest request){
        AjaxJson j = new AjaxJson();
        Enumeration<?>  keys = request.getParameterNames();
        Map<String,Object> map = Maps.newHashMap();
        while(keys.hasMoreElements()){
        	String reqCode = keys.nextElement().toString();
        	map.put(reqCode,request.getParameter(reqCode));
        }
        try {
			menuService.addMenuOrUpdate(map);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("系統忙，新增或更新菜单【"+map.get("menuNum")+"】失败!");
			logger.error("新增或者更新菜单【"+map.get("menuNum")+"】失败!",e);
		}
        if (logger.isDebugEnabled()) {
            logger.debug("请求【/metro/menu/addMenu】处理,响应对象【"+j+"】");
        }
        return j;
    }

    /**
     * @return
     * 删除一个菜单
     * @throws Exception 
     */
    @RequestMapping(value = "/delMenu", method = RequestMethod.GET)
    @ResponseBody
    public AjaxJson delMenu(@RequestParam("menuNum") String menuNum) {
        AjaxJson j = new AjaxJson();
        try {
			menuService.deleteMenu(menuNum);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("系統忙，删除菜单【"+menuNum+"】失败!");
			logger.error("删除菜单【"+menuNum+"】失败!",e);
		}
        if (logger.isDebugEnabled()) {
            logger.debug("请求【/metro/menu/delMenu】处理,响应对象【"+j+"】");
        }
        return j;
    }
    
    /**
     * @return
     * ICON图标列表页面
     */
    @RequestMapping(value = "/toIconPage", method = RequestMethod.GET)
    public String toIconPage(){
        if (logger.isDebugEnabled()) {
            logger.debug("请求【/metro/menu/toIconPage】处理页面【/view/metro/iconList.jsp】");
        }
        return "metro/iconList";
    }
}